local more1__shiren = fk.CreateSkill{
  name = "more1__shiren",
}
Fk:loadTranslationTable{
  ["more1__shiren"] = "识人",
  [":more1__shiren"] = "你的手牌上限+X。"..
  "出牌阶段各限一次，你可以与你攻击范围内的一名其他角色进行拼点，若你赢，则该角色的势力变为蜀势力;"..
  "你可以将至少X张红色牌置入牌顶堆里对至多X名角色造成1点伤害。(X为场上蜀势力角色的数量)",
  ["more1__shiren_pingdian"] = "拼点",
  ["more1__shiren_card"] = "将红色牌置入牌堆顶",
  ["#more1__shiren_pingdian"] = "选择一名其他角色进行拼点",
  ["#more1__shiren_card"] = "将至少 %arg 张红牌置入牌堆顶",
  ["#more1__shiren"] = "选择一项执行",
}

more1__shiren:addEffect("maxcards", {
  correct_func = function(self, player)
    if player:hasSkill(more1__shiren.name) then
      local hmax = 0
      for _, p in ipairs(Fk:currentRoom().alive_players) do
        if p.kingdom == "shu" then
          hmax = hmax + 1
        end
      end
      return hmax
    end
  end,
})

more1__shiren:addEffect("active", {
  anim_type = "support",
  prompt = "#more1__shiren",
  interaction = function(self, player)
    local choices = {"more1__shiren_pingdian" , "more1__shiren_card"}
    local cards = player:getCardIds("hej")
    local red = table.filter(cards, function (id)
      return Fk:getCardById(id).color == Card.Red
    end)

    local hmax = 0
    for _, p in ipairs(Fk:currentRoom().alive_players) do
      if p.kingdom == "shu" then
        hmax = hmax + 1
      end
    end    

    local n = 0 
    for _, p in ipairs(Fk:currentRoom().alive_players) do
      if player:canPindian(p) and player:inMyAttackRange(p)  then
        n = n + 1
      end
    end

    if player:getMark("more1__shiren_pingdian-phase") > 0  then
      table.removeOne(choices, "more1__shiren_pingdian")
    end
    if n == 0 then
      table.removeOne(choices, "more1__shiren_pingdian")
    end

    if player:getMark("more1__shiren_card-phase") > 0 then
      table.removeOne(choices, "more1__shiren_card")
    end
    if #red < hmax then
      table.removeOne(choices, "more1__shiren_card")
    end
    if hmax == 0 then
      table.removeOne(choices, "more1__shiren_card")
    end

    if #choices == 0 then return false end
    return UI.ComboBox { choices = choices }
  end,
  can_use = function (self, player)
    local cards = player:getCardIds("hej")
    local red = table.filter(cards, function (id)
      return Fk:getCardById(id).color == Card.Red
    end)

    local hmax = 0
    for _, p in ipairs(Fk:currentRoom().alive_players) do
      if p.kingdom == "shu" then
        hmax = hmax + 1
      end
    end    

    local n = 0 
    for _, p in ipairs(Fk:currentRoom().alive_players) do
      if player:canPindian(p) and player:inMyAttackRange(p)  then
        n = n + 1
      end
    end

    return player:hasSkill(more1__shiren.name, true) and
    ((player:getMark("more1__shiren_pingdian-phase") == 0 and n > 0) or (player:getMark("more1__shiren_card-phase") == 0 and #red >= hmax ))
  end,
  target_num = 0,
  card_num = 0,
  on_use = function(self, room, effect)
    local from = effect.from
    if self.interaction.data == "more1__shiren_pingdian" then
      local success, dat = room:askToUseActiveSkill(from, {
        skill_name = "more1__shiren_pingdian_active",
        prompt = "#more1__shiren_pingdian",
        cancelable = false,
      })
      if success and dat then 
        local to = dat.targets[1]
        local pindian = from:pindian({to}, more1__shiren.name)
        if from.dead then return end
        if pindian.results[to].winner == from then
          room:changeKingdom(to, "shu", true)
        end
        room:addPlayerMark(from,"more1__shiren_pingdian-phase",1)
      end

    else
      local hmax = 0
      for _, p in ipairs(Fk:currentRoom().alive_players) do
        if p.kingdom == "shu" then
          hmax = hmax + 1
        end
      end
      local success, dat = room:askToUseActiveSkill(from, {
        skill_name = "more1__shiren_card_active",
        prompt = "#more1__shiren_card:::"..hmax,
        cancelable = false,
      })
      if success and dat then 
        local to = dat.targets
        local cards = dat.cards

        if #cards > 1 then
          cards = room:askToGuanxing(from, {
            cards = cards,
            top_limit = { #cards, #cards },
            bottom_limit = { 0, 0 },
            skill_name = more1__shiren.name,
            skip = true,
          }).top
          cards = table.reverse(cards)
        end

        room:moveCardTo(cards, Card.DrawPile, nil, fk.ReasonPut, more1__shiren.name, nil, true)

        for _, p in ipairs(to) do
          room:damage({
            from = from,
            to = p,
            damage = 1,
            damageType = fk.NormalDamage,
            skillName = more1__shiren.name,
          })
        end
        room:addPlayerMark(from,"more1__shiren_card-phase",1)
      end
    end
  end,
})




return more1__shiren